package com.pd.spider.index;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
public class Controller {
    @Resource(name="resultRepository")
    ResultRepository resultRepository;

    @GetMapping("/pd_spider/getSortList")
    public List<HashMap<String, Object> > pd_getSortList()
    {
        return resultRepository.GetSortList();
    }

    @GetMapping("/pd_spider/getNameList")
    public List<HashMap<String, Object> > pd_getNameList(@RequestParam("sort") String sort)
    {
        return resultRepository.GetNameList(sort);
    }

    @PostMapping("/pd_spider/InsertTask")
    public int pd_InsertTask(@RequestBody Map<String,String> params)
    {
        return resultRepository.InsertTask(params.get("object"),params.get("sort"),params.get("name"));
    }

    @PostMapping("/pd_spider/InsertCompany")
    public int pd_InsertCompany(@RequestBody Map<String,String> params)
    {
        return resultRepository.InsertCompany(params.get("name"),params.get("url"));
    }

    @GetMapping("/pd_spider/GetAllTask")
    public List<HashMap<String, Object> > pd_getTaskList()
    {
        return resultRepository.GetAllTask();
    }

    @GetMapping("/pd_spider/GetBrand")
    public HashMap<String, Object> pd_getBrand()
    {
        HashMap<String, Object> re = new HashMap<>();
        re.put("brand_company",resultRepository.GetCompany("PD_SPIDER_BRAND","COMPANY_NAME"));
        re.put("brand_describe",resultRepository.GetCompany("PD_SPIDER_BRAND","INFRINGE_DESCRIPT"));
        return re;
    }

    @GetMapping("/pd_spider/GetPatent")
    public HashMap<String, Object> pd_getPatent()
    {
        HashMap<String, Object> re = new HashMap<>();
        re.put("brand_company",resultRepository.GetCompany("PD_SPIDER_PATENT","NAME"));
        re.put("brand_describe",resultRepository.GetCompany("PD_SPIDER_PATENT","PREDICT"));
        return re;
    }

    private int pd_getTaskID(@RequestParam("object") String object,@RequestParam("name") String name)
    {
        return resultRepository.GetTaskID(object,name);
    }

    @GetMapping("/pd_spider/GetResult")
    public HashMap<String, Object> pd_getResult(@RequestParam("table") String table,@RequestParam("object") String object,@RequestParam("name") String name,@RequestParam("page") int page) {
        String tableName = "";
        switch (table)
        {
            case "视频":
                tableName = "PD_SPIDER_VIDEO";
                break;
            case "购物":
                tableName = "PD_SPIDER_SHOPS";
                break;
        }
        int id = pd_getTaskID(object, name);
        return ShowPages(resultRepository.GetResult(tableName, id),page);
    }

    @GetMapping("/pd_spider/GetBrandResult")
    public HashMap<String, Object> pd_getBrandResult(@RequestParam("name") String name,@RequestParam("describe") String describe,@RequestParam("page") int page)
    {
        return ShowPages(resultRepository.GetBrandResult(name,describe),page);
    }

    @GetMapping("/pd_spider/GetPatentResult")
    public HashMap<String, Object> pd_getPatentResult(@RequestParam("name") String name,@RequestParam("describe") String describe,@RequestParam("page") int page)
    {
        return ShowPages(resultRepository.GetPatentResult(name,describe),page);
    }

    private HashMap<String, Object> ShowPages(List<HashMap<String, Object> > list,int page)
    {
        int size = list.size();
        int pageSize = size % 10 == 0 ? size / 10 : size / 10 + 1;
        HashMap<String, Object> re = new HashMap<>();
        re.put("result", list.subList((page - 1) * 10, Math.min(page * 10, size)));
        re.put("size", pageSize);
        re.put("max",size);
        return re;
    }
}
